
//提交按钮
var $resetDate = $('.win-category .btn-re'),
	$getDataBtn = $('.win-category .btn-conf');

//1.品类弹窗
var $addCategory = $('.user-center .company-activity .addbtn'),
	$win_category = $('.win-category');
	
getchild(1);
getchild(2);


//添加品类时使用
var firstLevel = null;
//选中的品类    品类编辑时使用
var selectedCategory = {
    first: null,
    second: [],
    third: []
};



$addCategory.on('click',function(){
	$win_category.fadeIn();
	overfhidden();
	
	//获取品类数据
	$.ajax({
		type:"get",
		url:"http://v4.www.t3.laisj.com/publics/base/productTagTree",
		async:true,
		success:function(res){
            changeList('.first .first-cate',res,1);
		},
		error:function(res){
			console.log(res);      //错误的对象
		}
	});
});
//2.关闭品类弹窗
$('.win-category .btn-close').click(function(){
	$resetDate.trigger('click');
})
//取消按钮
$resetDate.click(function(){
	$('.win-category .category input[type="reset"]').trigger('click');
	$win_category.fadeOut();
});
//提交
$getDataBtn.click(function(){
	console.log(selectedCategory);
})




//1.1 修改添加dom                  index : 
function changeList(selector,data,index){
	var datahtml = '',
		id = null,
		$checked = null;
	
	//一二三级品类所有选项
	var $firstSelect = null,
		$secondSelect = null,
		$thirdSelect = null;
	
	
	//生成dom元素      index      1:一级品类  
	if(index==1){
		for(var i=0;i<data.length;i++){
			datahtml = datahtml + listItem('first-cate',i,data,index);
		}
		$(selector).html(datahtml);
		
		//标记选中的二级品类
		$firstSelect  = $(selector+' input[name="first-cate"]');
		
		
		
		$firstSelect.change(function(){
			$checked = $(selector+' input[name="first-cate"]:checked');
			id =  $checked.data('val');
			
			//记录选中的一级品类
			selectedCategory.first = $checked.val();
			//改变一级品类时，清除二级、三级品类
			getchild(index);
			
			return changeList('.second .second-cate',data[parseInt(id)].children,2);
		});
	}else if(index==2){
		
		for(var i=0;i<data.length;i++){
			datahtml = datahtml + listItem('second-cate',i,data,index);
		}
		$(selector).html(datahtml);
		
		//标记选中的二级品类
		$secondSelect  = $(selector+' input[name="second-cate"]');
		selectCategory(selectedCategory.second,$secondSelect);
		
		//change   事件
		$secondSelect.change(function(){
			//清空记录选中的二级品类
			selectedCategory.second = [];
			
			//改变二级品类时，清除三级品类
			getchild(index);
			
			$(this).parent().toggleClass('checked');
			var $checked = $(selector+' input[name="second-cate"]:checked');
			for(var i = 0;i<$checked.length;i++){
				id =  $checked[i].dataset.val;
				//记录选中的二级品类
				selectedCategory.second.push($checked.eq(i).val());
				changeList('.third',data[parseInt(id)],3);
			}
		});
	}else{
		if(data.children.length>0){
			var datachild = data.children;
			datahtml = '<div class="third-cate">'+
                          	'<p>'+data.name+'</p>'+
                            '<div class="checkbox-group clearfix">';
			for(var i=0;i<datachild.length;i++){
				datahtml = datahtml + listItem('third-cate',i,datachild,index);
			}
			datahtml = datahtml + '</div>\n</div>';
			$(selector).append(datahtml);
			
			
			//标记选中的三级品类
			$thirdSelect  = $(selector+' input[name="third-cate"]');
			selectCategory(selectedCategory.third,$thirdSelect);
			
			
			//change事件
			$thirdSelect.change(function(){
				console.log($(this).parent());
				$(this).parent().toggleClass('checked');
				//判断是否选择了末级品类
				var $thirdSelected = $(selector+' input[name="third-cate"]:checked');
				if($thirdSelected.length>0){
					changeBtn(true);
				}else{
					changeBtn(false);
				}
				//记录选中的二级品类
				selectedCategory.third = [];
				for(var i = 0;i<$thirdSelected.length;i++){
					selectedCategory.third.push($thirdSelected.eq(i).val());
				}
			});
		}else{
			getchild(index);
		}
	}
	return selectedCategory;
}

//1.2 三级不同的品类
function listItem(selname,i,data,index){
	var datahtml = '';
	//index   1:  一级品类   ,   2:  二级品类    ,  3:  三级品类
	if(index==1){
		datahtml = datahtml+'<li data-type="'+selname+'">\n' +
								'<div class="radio-wrap">\n' +
									'<span class="radio_btn">\n' +
					                    '<input type="radio" name="'+selname+'" id="'+selname+i+'" value="'+data[i].id+'" data-val="'+i+'" />\n' +
					                    '<span class="center"></span>\n' +
					                    '<span class="border"></span>\n' +
					                '</span>\n' +
					            	'<label class="check-text" for="'+selname+i+'">'+data[i].name+'</label>\n' +
					            '</div>\n' +
					        '</li>';
	}else if(index==2){
		datahtml = datahtml + '<li data-type="'+selname+'">\n' +
								    '<div class="check-wrap">\n' +
					                    '<input type="checkbox" name="'+selname+'" id="'+selname+i+'" value="'+data[i].id+'" data-val="'+i+'" />\n' +
						            	'<label class="check-text" for="'+selname+i+'"><span class="check"></span>'+data[i].name+'</label>\n' +
						            '</div>\n' +
						       '</li>';
	}else if(index==3){
		datahtml = datahtml + '<div class="check-wrap">\n' +
				                    '<input type="checkbox" name="'+selname+'" id="'+selname+i+'" value="'+data[i].id+'" data-val="'+i+'"  />\n' +
					            	'<label class="check-text" for="'+selname+i+'"><span class="check"></span>'+data[i].name+'</label>\n' +
					            '</div>\n';
	}
    return datahtml;            
}








//1.3  change品类视图
function getchild(index){
    if(index==1){
        $('.win-category .second-cate').html('');
        $('.win-category .third').html('');
        changeBtn(false);
    }else if(index==2){
        $('.win-category .third').html('');
    }else{
    	 //末级品类
        console.log('no more');
        changeBtn(true);
    }
}







//1.4改变添加按钮   true:可点击    false:不可点击
function changeBtn(off){
    if(off){
        $getDataBtn.addClass('active');
        $getDataBtn.removeAttr('disabled');
        $getDataBtn.css('cursor','pointer');
    }else{
        $getDataBtn.removeClass('active');
        $getDataBtn.attr('disabled','true');
        $getDataBtn.css('cursor','not-allowed');
    }
}




//1.5编辑已选中的品类/已禁用的品类    二级、三级品类
function selectCategory(selectedData,category){
	if(selectedData.length>0){
		for(var i = 0;i<selectedData.length;i++){
			for(var j = 0;j<category.length;j++){
				if(selectedData[i]==category.eq(j).val()){
					//标记已选中
					category.eq(j).parent().addClass('checked');
				}
			}
		}
		//判断是禁用还是   标记已选中 : true
//		if(off){
//			
//		}else{
//			for(var i = 0;i<selectedData.length;i++){
//				for(var j = 0;j<category.length;j++){
//					if(selectedData[i]==category.eq(j).val()){
//						//已经选定过了的选项禁用
//						category.eq(j).parent().css('cursor','not-allowed').attr('disabled','true');
//					}
//				}
//			}
//		}
	}
}





//1.6一级品类标记
function firstselfun(firstselected,firstlist){
	if(firstselected !=null){
		for(var i = 0;i<firstlist.length;i++){
			if(firstlist.eq(i).val() == firstselected){
				firstlist.eq(i).trigger('click');
			}
		}
	}
}








//2.
function category(){
	this.dialogVisible = alse;
	this.isSelectLast = false;
  	this.selectedFirst = null;
  	this.selectedSecond = [];
  	this.selectedThird = [];
  	this.firstLevels = [];
  	this.secondLevels = [];
  	this.thirdLevels = [];
  	this.categoryForm = {
	    first: null,
	    second: [],
	    third: []
  	};
  	this.results = {};
  	
  	this.getCategoryData = function(){
  		
  	};
  	
  	
}
